package com.czb.servlet;

import com.czb.service.LogisticsService;
import com.czb.service.LogisticsServiceImpl;
import com.czb.util.PageBean;
import com.czb.util.ResultJson;
import com.czb.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/LogisticsServlet")
public class LogisticsServlet extends HttpServlet {
    LogisticsService logisticsService = new LogisticsServiceImpl();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        if(action.equals("findAll")){
            findAll(req,resp);
        }else if(action.equals("add")){
            add(req,resp);
        }else if(action.equals("update")){
            update(req,resp);
        }else if(action.equals("delete")){
            delete(req,resp);
        }else if(action.equals("findById")){
            findById(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        int loid = Integer.valueOf(req.getParameter("loid"));
        String sql = "delete from logistics where loid = ?";
        int i = logisticsService.delete(sql,loid);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        int loid = Integer.valueOf(req.getParameter("loid"));
        String loname = req.getParameter("loname");
        String loidcard = req.getParameter("loidcard");
        String lotel = req.getParameter("lotel");
        String lotime = req.getParameter("lotime");
        Object[] obj = {loname,loidcard,lotel,lotime,loid};
        String sql = "update logistics set loname = ?,loidcard = ?,lotel = ?,lotime = ? where loid = ?";
        int i = logisticsService.update(sql,obj);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) {
        int loid = Integer.valueOf(req.getParameter("loid"));
        String sql  ="select *  from logistics where loid = ?";
        Map map = logisticsService.findById(sql,loid);
        ResultJson success = ResultJson.success(map);
        Utils.returnJson(success,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        String loname = req.getParameter("loname");
        String loidcard = req.getParameter("loidcard");
        String lotel = req.getParameter("lotel");
        String lotime = req.getParameter("lotime");
        Object[] obj = {loname,loidcard,lotel,lotime};
        String sql = "insert into logistics values(null,?,?,?,?)";
        int i = logisticsService.add(sql,obj);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = req.getParameter("page") != null ? Integer.valueOf(req.getParameter("page")) : 1;
        Integer limit = req.getParameter("limit") != null ? Integer.valueOf(req.getParameter("limit")) : 10;
        String  loname =req.getParameter("loname")!=null?"%"+req.getParameter("loname")+"%":"%%";
        String  loidcard =req.getParameter("loidcard")!=null?"%"+req.getParameter("loidcard")+"%":"%%";
        String sql = "select *  from logistics where 1=1 and loname like ? and loidcard like ?";
        PageBean<Map> pageBean = logisticsService.findAll(page,limit,sql,loname,loidcard);
        System.out.println(pageBean);
        ResultJson success = ResultJson.success(pageBean);
        Utils.returnJson(success,resp);
    }
}
